﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;

namespace ReadFile
{
    public struct g_Triple
    {
        public int user;
        public int article;
        public double freq;
    };
    class Program
    {

        
        static void Main(string[] args)
        {
            ArrayList xuhaoresult = new ArrayList();
            StreamReader objReader = new StreamReader("D:\\xiaojun.txt");
            string sline = "";
            int linenumber = 1;
            while (sline != null)
            {
                sline = objReader.ReadLine();
                if (sline != null && !sline.Equals(""))
                {
                    string[] trash = sline.Split(',');
                    string[] temp = trash[1].Split(';');
                    {
                        int length = temp.Length;
                        for (int i = 0; i < length; ++i)
                        {
                            int l_article = int.Parse(temp[i]);
                            double l_freq = double.Parse(temp[i + 1]);
                            g_Triple trp = new g_Triple();
                            trp.user = linenumber;
                            trp.article = l_article;
                            trp.freq = l_freq;
                            xuhaoresult.Add(trp);
                        }
                    }
                }
                ++linenumber;
            }
            objReader.Close();



            ArrayList xiaojunresult = new ArrayList();
            StreamReader objReader2 = new StreamReader("D:\\filescore.txt");
            string sline2 = "";
            int usrnum = 0;
            while (sline2 != null)
            {
                sline2 = objReader2.ReadLine();
                if (sline2 != null && !sline2.Equals(""))
                {
                    if (sline2.Contains("user"))
                    {
                        usrnum++;
                        continue;
                    }
                    string[] temptrim = sline2.Split(' ');
                    int l_arti = int.Parse(temptrim[0]);
                    double l_fre = double.Parse(temptrim[1]);
                    g_Triple trip = new g_Triple();
                    trip.user = usrnum;
                    trip.article = l_arti;
                    trip.freq = l_fre;
                    xiaojunresult.Add(trip);
                }
            }
            objReader2.Close();

            int[] recommendarticle = new int[5];
            int[] articlecount1 = new int[200];
            double[] frequencycount1 = new double[200];
            int[] articlecount2 = new int[200];
            double[] frequencycount2 = new double[200];
            int pos1, pos2;
            double factor = 1.1;
            int what1 = 0;
            int what2 = 0;
            int what3 = 0;
            int what4 = 0;
            for (int k = 0; k < 200; ++k)
            {
                articlecount1[k] = 0;
                articlecount2[k] = 0;
                frequencycount1[k] = 0;
                frequencycount2[k] = 0;
            }
            pos1 = 0;
            pos2 = 0;
            FileStream fs = new FileStream("D:\\xuhao.txt", FileMode.Create);
            StreamWriter streamwriter = new StreamWriter(fs);
            for (int usernum = 1; usernum <= 5551; ++usernum )
            {
                for (int x = 0; x < xiaojunresult.Count; x++ )
                {
                    g_Triple temptr = (ReadFile.g_Triple)xiaojunresult[x];
                    if (temptr.user == usernum)
                    {
                        articlecount1[pos1] = temptr.article;
                        frequencycount1[pos1] = temptr.freq;
                        pos1++;
                    }
                }
                for (int y = 0; y < xuhaoresult.Count; y++)
                {
                    g_Triple temptrr = (ReadFile.g_Triple)xuhaoresult[y];
                    if (temptrr.user == usernum)
                    {
                        articlecount2[pos2] = temptrr.article;
                        frequencycount2[pos2] = temptrr.freq;
                        pos2++;
                    }
                }

                for (int u = 0; u < pos2; ++u )
                {
                    if (articlecount1.Contains(articlecount2[u]))
                    {
                        for (int e = 0; e < articlecount1.Length; e++ )
                        {
                            if (articlecount1[e] == articlecount2[u])
                            {
                                frequencycount1[e] += frequencycount2[u] * factor;
                            }
                        }
                        continue;
                    }
                    articlecount1[pos1] = articlecount2[u];
                    frequencycount1[pos1] = frequencycount2[u] * factor;
                    pos1++;
                }
                int[] articlepos = new int[5];
                int temppos = 0;
                if (pos1 > 5)
                {

                    for (int i = 0; i < pos1; ++i)
                    {
                        if (temppos == 5)
                        {
                            break;
                        }
                        if (frequencycount1[i] == frequencycount1.Max())
                        {
                            articlepos[temppos] = i;
                            temppos++;
                            frequencycount1[i] = -1;
                            i = -1;
                        }
                    }
                    for (int i = 0; i < recommendarticle.Length; i++ )
                    {
                        recommendarticle[i] = articlecount1[articlepos[i]];
                    }
                }
                else 
                {
                    for (int i = 0; i < pos1; ++i)
                    {
                        if (temppos == pos1)
                        {
                            break;
                        }
                        if (frequencycount1[i] == frequencycount1.Max())
                        {
                            articlepos[temppos] = i;
                            temppos++;
                            frequencycount1[i] = -1;
                            i = -1;
                        }
                    }
                    for (int i = 0; i < pos1; i++)
                    {
                        recommendarticle[i] = articlecount1[articlepos[i]];
                    }
                    Random ro = new Random(); 
                    while(pos1 < 5)
                    {
                        int art = ro.Next(16980);
                        if(!recommendarticle.Contains(art))
                        {
                            recommendarticle[pos1] = art;
                            pos1++;
                        }
                    }
                }
                
                string ans = "";
                ans = usernum.ToString() + ",";
                for (int i = 0; i < recommendarticle.Length; i++ )
                {
                    ans += recommendarticle[i].ToString() + ";";
                }
                streamwriter.WriteLine(ans);
                streamwriter.Flush();


 
                for (int k = 0; k < 200; ++k)
                {
                    articlecount1[k] = 0;
                    articlecount2[k] = 0;
                    frequencycount1[k] = 0;
                    frequencycount2[k] = 0;
                }
                for (int k = 0; k < 5; ++k )
                {
                    recommendarticle[k] = 0;
                }
                pos1 = 0;
                pos2 = 0;
            }

        }


        
    }
}
